<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/24
 * Time: 11:05
 */
class Model_Lottery extends PhalApi_Model_NotORM{
    protected function getTableName($id){
        return 't_prize';    //奖品表
    }

    public function getPrize($obj){  //用户去中奖
        $phone = $obj->phone;
        $time = date('Ymd',time());
        $ok = mt_rand(1,1000);
        if($ok>=500 && $ok<=1000){
            goto c;
        }elseif($ok>=1 && $ok<=499){
            goto a;
        }
        c:
        //1元券
        $sql="select * from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift like '1元' limit 1";
        $data =$this ->getORM()-> queryAll($sql,array());

        if(intval($data[0]['amount']) != 0){   //1元券不为空,奖品减1,
            $this->getORM()->where("gift=? and FROM_UNIXTIME(time,'%Y%m%d')=$time",'1元')
                ->update(array('amount' => new NotORM_Literal("amount - 1")));
            return 1;
        }else{
            goto a;
        }

        a:
        //没有中奖
         return 0;


    }
  /*
   * 判断奖池是否为空
   */
    public function giftCount(){
        $time = date('Ymd',time());
        $sql1="select amount from p_lottery where FROM_UNIXTIME(time,'%Y%m%d')=$time and gift = '1元'";

        $info1=$this->getORM()->queryAll($sql1,array());

        $num1=intval($info1[0]['amount']);

        $sum = $num1;
        return $sum;
    }
}